<template>
	<view class="content">
		<view class=" padding">
			<view class="padding ffffff border">
				<view class="flexbetween">
					<view class="text_x color_ling">新手机号</view>
					<input type="number" placeholder="请输入新手机号" v-model="mobile" class="inp text_a"
						style="text-align: right;">
				</view>
			</view>
			<view class="padding ffffff margin_top flexbetween border">
				<view class="text_x color_ling">验证码</view>
				<view class="flexleft">
					<input type="number" v-model="captcha" placeholder="请输入验证码" class="text_a  inp">
					<view class="orange text_a" @click.stop="sendcode">{{codetext}}</view>
				</view>
			</view>
		</view>
		<view class="padding">
			<view class="btn" @click="show = true">确认更换</view>
		</view>
		<u-modal :show="show" cancelColor="#555" confirmColor="#ff7510" :showCancelButton="true" :title="title"
			@cancel="cancel" @confirm="confirm"></u-modal>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				show: false,
				mobile: '',
				title: '确定要修改手机号么？',
				captcha: "", // 手机验证码
				codetext: "获取验证码",
			}
		},
		methods: {
			// 发送验证码
			sendcode() {
				let _this = this
				let mobile = _this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				httpRequest.request('/api/sms/send', 'GET', {
					mobile: mobile,
					event: "changemobile"
				}).then(resp => {
					if (resp.code == 1) {
						var i = 60;
						var timer = setInterval(function() {
							_this.codetext = i + "秒";
							if (i < 1) {
								clearInterval(timer);
								_this.codetext = "获取验证码";
							} else {
								i--;
							}
						}, 1000)

					} else if (resp.code == 0) {
						httpRequest.toast(resp.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				})
			},
			cancel() {
				this.show = false
			},
			confirm() {
				this.show = false;
				if (this.mobile == '') {
					httpRequest.toast('请输入手机号');
					return false;
				}
				if (!this.mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if (this.captcha == '') {
					httpRequest.toast('请输入验证码');
					return false;
				}
				this.changeMobile()
			},
			// 修改手机号
			changeMobile() {
				let _this = this
				httpRequest.request('/api/user/changemobile', 'GET', {
					mobile: _this.mobile,
					captcha: _this.captcha
				}, false, false, true).then(res => {
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
						httpRequest.toast(res.msg);
					} else if (res.code == 0) {
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.inp{
		text-align: right;
	}
	.color_ling{
		min-width: 120rpx;
	}

	.orange {
		color: rgba(212, 139, 57, 1);
		min-width: 170rpx;
		text-align: center;
	}

	.btn {
		position: fixed;
		bottom: 50rpx;
		width: 90vw;
		color: #fff;
		background-color: RGBA(212, 139, 57, 1);
		padding: 20rpx 0;
		text-align: center;
		border-radius: 64rpx;
		z-index: 100;
	}
</style>